package com.shkco.pet.service.impl;

import com.shkco.basic.service.IBaiduAiAuditService;
import com.shkco.basic.util.AjaxResult;
import com.shkco.basic.util.LoginContext;
import com.shkco.org.domain.Employee;
import com.shkco.org.mapper.EmployeeMapper;
import com.shkco.org.service.IEmployeeService;
import com.shkco.pet.domain.Pet;
import com.shkco.pet.domain.PetDetail;
import com.shkco.pet.domain.PetOnlineAuditLog;
import com.shkco.pet.mapper.PetDetailMapper;
import com.shkco.pet.mapper.PetMapper;
import com.shkco.pet.mapper.PetOnlineAuditLogMapper;
import com.shkco.pet.service.IPetService;
import com.shkco.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    @Transactional
    public void add(Pet pet) {
        super.add(pet);

        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);
        }
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.update(petDetail);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        super.del(id);
        petDetailMapper.deleteByPetId(id);
    }


    @Override
    @Transactional
    public void patchDel(List<Long> ids) {
        super.patchDel(ids);
        petDetailMapper.patchDeleteByPetId(ids);
    }

    @Override
    @Transactional
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        // 对每一个宠物都要审核：审核name和resources
        for (Long id : ids) {
            Pet pet = petMapper.findById(id);
            // 审核文本
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            //审核图片
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());
            // 公共的信息：
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setPetId(id);
            //获取登陆信息id
            Long logininfoId = LoginContext.getLogininfo(request).getId();
            Employee employee = employeeMapper.loadByLogininfoId(logininfoId);
            log.setAuditId(employee.getId());

            if(boo1 && boo2){//文本和图片都审核成功
                //2.审核通过：改状态和时间 + 记录审核日志，状态改为1 - 上架
                petMapper.onsale(new Date(),id);

                log.setState(1);//1表示审核成功
                log.setNote("审核成功");//审核日志
                petOnlineAuditLogMapper.add(log);

            }else{
                //3.审核失败：记录审核日志
                log.setState(0);//0表示审核失败
                log.setNote("审核失败，图片和文本非法!!!");//审核日志
                petOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setSuccess(false).setMsg("审核失败，图片或文本非法!");
            }
        }
        return new AjaxResult();
    }

    @Override
    public void offsale(List<Long> ids, HttpServletRequest request) {
        petMapper.offsale(new Date(),ids);
    }
}
